<?php
namespace app\api\controller\biliao;
use app\api\controller\Common;
use think\Db;
/*
 融云为了客户隐私考虑，既不同步又不保存用户的好友关系。
 所以，所有用户的好友关系都需要开发者自己实现、管理维护。
*/
class User extends Common{  //直接继承外部common 不适用融云sdk的方法
	public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }
    public function _initialize(){
        //运行上级初始化方法
        parent::_initialize();
        //币聊表数据
        $biliao=db('biliao')->where('uid',$this->userinfo['id'])->find();
        if(!$biliao){
            apifasong(['zt'=>'0','msg'=>'非法操作 币聊还未注册']);
        }else{
            $this->biliao=$biliao;
        }
    }
    public function base(){   //获取基本信息，确定已申请token，不用访问index/base，不调用融云插件，节约服务器性能
        $biliao = $this->biliao;    //币聊表数据
        $biliao['frs']=frs_info($biliao['frs']);
        $biliao['frsadd']=frsadd_info($biliao['frsadd']);
        $biliao['discussion']=discussion_info($biliao['discussion']);
        $biliao['group']=group_info($biliao['group']);
        $biliao['chatroom']=chatroom_info($biliao['chatroom']);
        apifasong(['zt'=>'1','biliao'=>$biliao]);
    }
    public function get_name_img_byid(){   //根据用户id获取用户显示名字和头像（用于群聊显示不认识人的名字头像）
        $appdata = $this->appdata;  //接收的所有数据 含header
        $biliao = $this->biliao;    //币聊表数据
        $frs = $biliao['frs']?array_column(json_decode($biliao['frs'],true), NULL, 'u'):''; //id做健名好友数组
        if(isset($appdata['uid'])){
            $uinfo = db('user')->where('id',$appdata['uid'])->field('uname,nicename,truename,headimg')->find();
            if($uinfo){
                $result['name'] = ($frs&&isset($frs[$appdata['uid']])&&$frs[$appdata['uid']]['n'])?$frs[$appdata['uid']]['n']:(($uinfo['nicename']?:$uinfo['truename'])?:$uinfo['uname']);  //备注名字》（对方群昵称）对方昵称》对方真实姓名》对方账号
                $result['headimg'] = $uinfo['headimg'];
                apifasong(['zt'=>'1','result'=>$result]);
            }else{
                apifasong(['zt'=>'0','uinfo'=>'']);
            }
        }else{
            apifasong(['zt'=>'0','uinfo'=>'']);
        }
    }
    public function get_name_img_byids(){   //根据用户ids获取用户显示名字和头像（用于群聊显示不认识人的名字头像）
        $appdata = $this->appdata;  //接收的所有数据 含header
        $biliao = $this->biliao;    //币聊表数据
        $frs = $biliao['frs']?array_column(json_decode($biliao['frs'],true), NULL, 'u'):''; //id做健名好友数组
        if(isset($appdata['uids'])){
            $uids = explode(',',$appdata['uids']);
            foreach ($uids as $uid) {
                $uinfo = db('user')->where('id',$uid)->field('uname,nicename,truename,headimg')->find();
                if($uinfo){
                    $result[$uid]['name'] = ($frs&&isset($frs[$uid])&&$frs[$uid]['n'])?$frs[$uid]['n']:(($uinfo['nicename']?:$uinfo['truename'])?:$uinfo['uname']);  //备注名字》（对方群昵称）对方昵称》对方真实姓名》对方账号
                    $result[$uid]['name'] = ($uinfo['nicename']?:$uinfo['truename'])?:$uinfo['uname'];
                    $result[$uid]['headimg'] = $uinfo['headimg'];
                }else{
                    $result[$uid]['name'] = '';
                    $result[$uid]['headimg'] = '';
                }
            }
            apifasong(['zt'=>'1','result'=>$result]);
        }else{
            apifasong(['zt'=>'0','result'=>'']);
        }
    }
    public function changeonline(){    //用户上下线状态标记
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $biliao = $this->biliao;    //币聊表数据
        if(isset($appdata['state'])){
            $ret = db('biliao')->where('uid',$userinfo['id'])->update(['online'=>$appdata['state']]);
            apifasong(['zt'=>'1','ret'=>$ret]);
        }
    }
    public function sousuo(){   //搜索指定好友
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $biliao = $this->biliao;    //币聊表数据
        //必填项
        if(!isset($appdata['keywords'])){
            apifasong(['zt'=>'0','msg'=>'搜索内容为空']);
        }
        //查询
        $ret = db('biliao')->where('uname',$appdata['keywords'])->find();
        if(!$ret){
            apifasong(['zt'=>'0','msg'=>'该用户不存在或未开通币聊']);
        }else{
            $userinfo_ss=userinfobyuname($ret['uname']);
            $info['uid'] = $ret['uid'];
            $info['uname'] = $ret['uname'];
            $info['online'] = $ret['online']==1?'在线':'离线';
            $info['qianming'] = $ret['qianming']?$ret['qianming']:'什么也没有写';
            $info['headimg'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$userinfo_ss['headimg'];
            $info['nicename'] = $userinfo_ss['nicename']?$userinfo_ss['nicename']:'未设置';
            $info['sex'] = sex($userinfo_ss['sex']);
            apifasong(['zt'=>'1','info'=>$info]);
        }
    }
    public function frsadd(){   //申请添加指定好友
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $biliao = $this->biliao;    //币聊表数据
        //必填项
        if(!isset($appdata['uid'])){
            apifasong(['zt'=>'0','msg'=>'用户不存在']);
        }
        //判断好友-不能加自己-已是好友-黑名单
        if($appdata['uid']==$biliao['uid']){
            apifasong(['zt'=>'0','msg'=>'不能添加自己为好友']);
        }
        if($biliao['frs'] && !empty(json_decode($biliao['frs'],true)) && isset(array_column(json_decode($biliao['frs'],true), NULL, 'u')[$appdata['uid']])){
            apifasong(['zt'=>'0','msg'=>'已是您的好友']);
        }
        //执行好友申请
        $biliao_add = db('biliao')->where('uid',$appdata['uid'])->find();   //被添加人
        $frsadd_change = frsadd_change($biliao_add['frsadd'],$biliao['uid'],'jia',$appdata['msg']);
        $ret = db('biliao')->where('uid',$appdata['uid'])->update(['frsadd'=>$frsadd_change]);
        if(!$ret){
            apifasong(['zt'=>'0','msg'=>'申请失败']);
        }else{
            apifasong(['zt'=>'1','msg'=>'好友申请已发出']);
        }
    }
    public function frsadd_do(){   //处理指定好友申请-通过/拒绝
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $biliao = $this->biliao;    //币聊表数据
        //必填项
        if(!isset($appdata['uid']) || !isset($appdata['ret'])){
            apifasong(['zt'=>'0','msg'=>'系统错误']);
        }
        //验证
        $bl_change = db('biliao')->where('uid',$appdata['uid'])->find();   //被处理人
        if(!$bl_change){
            apifasong(['zt'=>'0','msg'=>'联系人不存在']);
        }
        //处理业务
        if($appdata['ret']=='tongguo'){
            //通过-互加好友-删除申请信息
            $frs_change1 = frs_change($biliao['frs'],$bl_change['uid'],'jia');
            $frs_change2 = frs_change($bl_change['frs'],$biliao['uid'],'jia');
            $frsadd_change = frsadd_change($biliao['frsadd'],$bl_change['uid'],'jian');
            Db::startTrans();   // 启动事务-------------
            $ret1 = db('biliao')->where('uid',$biliao['uid'])->update(['frsadd'=>$frsadd_change,'frs'=>$frs_change1]);
            $ret2 = db('biliao')->where('uid',$bl_change['uid'])->update(['frs'=>$frs_change2]);
            if($ret1 && $ret2){
                Db::commit();       // 提交事务-------------
                apifasong(['zt'=>'1','msg'=>'处理成功']);
            }else{
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'处理失败']);
            }
        }elseif($appdata['ret']=='jujue'){
            //拒绝-删除申请信息
            $frsadd_change = frsadd_change($biliao['frsadd'],$appdata['uid'],'jian');
            $ret1 = db('biliao')->where('uid',$biliao['uid'])->update(['frsadd'=>$frsadd_change]);
            if($ret1){
                Db::commit();       // 提交事务-------------
                apifasong(['zt'=>'1','msg'=>'处理成功']);
            }else{
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'处理失败']);
            }
        }
    }
    
    public function edit_frsinfo(){    //修改好友备注信息
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $biliao = $this->biliao;    //币聊表数据

        //必填项
        if(!isset($appdata['u'])){
            apifasong(['zt'=>'0','msg'=>'系统错误']);
        }

        $frs = array_column(json_decode($biliao['frs'],true), NULL, 'u');
        if(!isset($frs[$appdata['u']])){
            apifasong(['zt'=>'0','msg'=>'系统错误']);
        }

        if(isset($appdata['n'])){   //备注名字
            $frs[$appdata['u']]['n'] = $appdata['n'];
        }elseif(isset($appdata['m'])){   //备注消息
            $frs[$appdata['u']]['m'] = $appdata['m'];
        }

        $frs = json_encode(array_values($frs));
        $ret = db('biliao')->where('uid',$biliao['uid'])->update(['frs'=>$frs]);
        if($ret){
            $biliao = db('biliao')->where('uid',$biliao['uid'])->find();    //币聊表数据
            $biliao['frs']=frs_info($biliao['frs']);
            $biliao['frsadd']=frsadd_info($biliao['frsadd']);
            $biliao['discussion']=discussion_info($biliao['discussion']);
            $biliao['group']=group_info($biliao['group']);
            $biliao['chatroom']=chatroom_info($biliao['chatroom']);

            apifasong(['zt'=>'1','msg'=>'修改成功','biliao'=>$biliao]);
        }else{
            apifasong(['zt'=>'0','msg'=>'修改失败']);
        }
    }

    public function discussion_edit(){    //修改讨论组信息
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $biliao = $this->biliao;    //币聊表数据

        //必填项
        if(!isset($appdata['discussionid'])){
            apifasong(['zt'=>'0','msg'=>'系统错误']);
        }

        //验证管理员
        // $discussion_sqlinfo=db('biliao_discussion')->where('discussionid',$appdata['discussionid'])->find();
        // if($discussion_sqlinfo['uid'] <> $userinfo['id']){
        //     apifasong(['zt'=>'0','msg'=>'你不是管理员']);
        // }

        //修改（公告在这修改 讨论组名字/权限等在融云修改 这里数据库不记录）
        if(isset($appdata['gonggao'])){   //群公告
            $ret = db('biliao_discussion')->where('discussionid',$appdata['discussionid'])->update(['gonggao'=>$appdata['gonggao']]);
        }elseif(isset($appdata['name_my'])){   //群昵称
            $discussion_sqlinfo=db('biliao_discussion')->where('discussionid',$appdata['discussionid'])->find();
            if($biliao['discussion']){
                $my_discussion = array_column(json_decode($biliao['discussion'],true), NULL, 'id'); //biliao表discussion字段数组，保存过群/改过群昵称会保存讨论组信息在这里
            }else{
                $my_discussion = [];
            }
            
            $my_discussion[$discussion_sqlinfo['id']]['id']=$discussion_sqlinfo['id'];
            $my_discussion[$discussion_sqlinfo['id']]['n']=$appdata['name_my'];

            $my_discussion = json_encode(array_values($my_discussion));
            $ret = db('biliao')->where('id',$biliao['id'])->update(['discussion'=>$my_discussion]);
        }
        if(isset($ret) && $ret){
            apifasong(['zt'=>'1','msg'=>'修改成功']);
        }else{
            apifasong(['zt'=>'0','msg'=>'修改失败']);
        }
    }
    public function discussion_create(){   //创建讨论组
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $biliao = $this->biliao;    //币聊表数据

        //必填项
        if(!isset($appdata['name']) || !isset($appdata['userIdList']) || !isset($appdata['discussionid'])){
            apifasong(['zt'=>'0','msg'=>'系统错误']);
        }
        //插入数据库
        $sqlid=db('biliao_discussion')->insertGetId([
            'uid'=>$userinfo['id'],
            'uname'=>$userinfo['uname'],
            'discussionid'=>$appdata['discussionid'],
            'time'=>time(),
            'gonggao'=>'无'
        ]);
        if($sqlid){
            apifasong(['zt'=>'1']);
        }else{
            apifasong(['zt'=>'0','msg'=>'系统错误']);
        }
    }

    public function discussion_sqlinfo(){   //获取讨论组信息
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $biliao = $this->biliao;    //币聊表数据

        //必填项
        if(!isset($appdata['discussionid'])){
            apifasong(['zt'=>'0','msg'=>'系统错误']);
        }
        //获取群sql信息
        $discussion_sqlinfo=db('biliao_discussion')->where('discussionid',$appdata['discussionid'])->find();
        
        
        if($discussion_sqlinfo){
            $creator = userinfobyid($discussion_sqlinfo['uid']);
            $discussion_sqlinfo['name_creator'] = $creator?(($creator['nicename']?:$creator['truename'])?:$creator['uname']):'群主账号异常';   //附加显示名字-群主
            $name_my = '';
            if($biliao['discussion']){
                $my_discussion = array_column(json_decode($biliao['discussion'],true), NULL, 'id'); //biliao表discussion字段数组，保存过群/改过群昵称会保存讨论组信息在这里
                if(isset($my_discussion[$discussion_sqlinfo['id']])){
                    $name_my = $my_discussion[$discussion_sqlinfo['id']]['n'];
                }
            }
            $discussion_sqlinfo['name_my'] = $name_my?:(($userinfo['nicename']?:$userinfo['truename'])?:$userinfo['uname']);   //附加显示名字-自己
            apifasong(['zt'=>'1','discussion_sqlinfo'=>$discussion_sqlinfo]);
        }else{
            apifasong(['zt'=>'0','msg'=>'资料获取失败']);
        }
    }

    public function discussion_quit(){  //退出群/被T出群 服务器处理
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $biliao = $this->biliao;    //币聊表数据

        //必填项
        if(!isset($appdata['discussionid']) || !isset($appdata['type'])){
            apifasong(['zt'=>'0','msg'=>'系统错误']);
        }

        //获取群sql信息
        $discussion_sqlinfo=db('biliao_discussion')->where('discussionid',$appdata['discussionid'])->find();

        if($appdata['type'] == 'quit'){ //主动退出讨论组
            if($biliao['discussion']){
                $my_discussion = array_column(json_decode($biliao['discussion'],true), NULL, 'id'); //biliao表discussion字段数组，保存过群/改过群昵称会保存讨论组信息在这里
                if(isset($my_discussion[$discussion_sqlinfo['id']])){
                    unset($my_discussion[$discussion_sqlinfo['id']]);
                    $my_discussion = json_encode(array_values($my_discussion));
                    $ret = db('biliao')->where('id',$biliao['id'])->update(['discussion'=>$my_discussion]);
                }
            }
        }elseif($appdata['type'] == 'remove'){  //被T出讨论组
            if($appdata['uid']){
                $biliao = db('biliao')->where('uid',$appdata['uid'])->find();
                if($biliao && $biliao['discussion']){
                    $my_discussion = array_column(json_decode($biliao['discussion'],true), NULL, 'id'); //biliao表discussion字段数组，保存过群/改过群昵称会保存讨论组信息在这里
                    if(isset($my_discussion[$discussion_sqlinfo['id']])){
                        unset($my_discussion[$discussion_sqlinfo['id']]);
                        $my_discussion = json_encode(array_values($my_discussion));
                        $ret = db('biliao')->where('id',$biliao['id'])->update(['discussion'=>$my_discussion]);
                    }
                }
            }
        }
        apifasong(['zt'=>'1','msg'=>'OK']);
    }
}






